local tuwei = fk.CreateSkill{
  name = "hanqing__tuwei",
  tags = { Skill.Limited },
}

Fk:loadTranslationTable{
  ["hanqing__tuwei"] = "图为",
  [":hanqing__tuwei"] = "限定技，当你成为牌目标后，你可以重铸一张牌、弃置两张牌、摸三张牌，令本回合使用【杀】的次数+X"..
  "（X为上述操作中包含【杀】的次数）；你击杀角色后重置此技能。",

  ["#hanqing__tuwei-invoke"] = "图为：你可以重铸一张牌、弃置两张牌、摸三张牌，令本回合使用【杀】的次数增加！",
}

tuwei:addEffect(fk.TargetConfirmed, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(tuwei.name) and
      player:usedSkillTimes(tuwei.name, Player.HistoryGame) == 0 and not player:isNude()
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local cards = room:askToCards(player, {
      min_num = 1,
      max_num = 1,
      include_equip = true,
      skill_name = tuwei.name,
      prompt = "#hanqing__tuwei-invoke",
      cancelable = true,
    })
    if #cards > 0 then
      event:setCostData(self, {cards = cards})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local banner = room:getBanner("hanqing__tuwei-turn") or 0
    local cards = event:getCostData(self).cards
    if table.find(cards, function (id)
      return Fk:getCardById(id).trueName == "slash"
    end) then
      banner = banner + 1
      room:setBanner("hanqing__tuwei-turn", banner)
    end
    room:recastCard(cards, player, tuwei.name)
    if player.dead then return end
    cards = room:askToDiscard(player, {
      min_num = 2,
      max_num = 2,
      include_equip = true,
      skill_name = tuwei.name,
      cancelable = false,
      skip = true,
    })
    if #cards > 0 then
      if table.find(cards, function (id)
        return Fk:getCardById(id).trueName == "slash"
      end) then
        banner = banner + 1
        room:setBanner("hanqing__tuwei-turn", banner)
      end
      room:throwCard(cards, tuwei.name, player, player)
      if player.dead then return end
    end
    cards = player:drawCards(3, tuwei.name)
    if #cards > 0 then
      if table.find(cards, function (id)
        return Fk:getCardById(id).trueName == "slash"
      end) then
        banner = banner + 1
        room:setBanner("hanqing__tuwei-turn", banner)
      end
    end
  end,
})

tuwei:addEffect(fk.Deathed, {
  mute = true,
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(tuwei.name, true) and data.killer == player and
      player:usedSkillTimes(tuwei.name, Player.HistoryGame) > 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    player:broadcastSkillInvoke(tuwei.name)
    player.room:notifySkillInvoked(player, self.name, "offensive")
    player:setSkillUseHistory(tuwei.name, 0, Player.HistoryGame)
  end,
})

tuwei:addEffect("targetmod", {
  residue_func = function (self, player, skill, scope, card, to)
    if Fk:currentRoom():getBanner("hanqing__tuwei-turn") and
      card and card.trueName == "slash" and scope == Player.HistoryPhase then
      return Fk:currentRoom():getBanner("hanqing__tuwei-turn")
    end
  end,
})

return tuwei
